/**
 * @file app_uv_pwm.c
 * @brief UV灯控制驱动实现
 * @details
 *   UV灯2根线控制:
 *   - PA8: 启动控制(GPIO,低电平=点亮,高电平=熄灭)
 *   - PB15: 亮度控制(TIM1_CH3N PWM,高电平有效)
 *
 * @date 2025-11-17
 */

#include "app_uv_pwm.h"

// 当前UV灯电流状态
static uint8_t s_uv_current_percent = 0;

/**
 * @brief UV灯初始化
 */
void app_uv_pwm_init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;

    // 使能时钟
    RCC_APB2PeriphClockCmd(UV_ENABLE_GPIO_RCC, ENABLE);  // PA8时钟
    RCC_APB2PeriphClockCmd(UV_PWM_GPIO_RCC, ENABLE);     // PB15时钟
    RCC_APB2PeriphClockCmd(UV_PWM_TIMER_RCC, ENABLE);    // TIM1时钟

    // 配置PA8为推挽输出(UV启动控制)
    GPIO_InitStructure.GPIO_Pin = UV_ENABLE_GPIO_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(UV_ENABLE_GPIO_PORT, &GPIO_InitStructure);
    GPIO_ResetBits(UV_ENABLE_GPIO_PORT, UV_ENABLE_GPIO_PIN);  // 初始高电平(熄灭)

    // 配置PB15为复用推挽输出(TIM1_CH3N PWM)
    GPIO_InitStructure.GPIO_Pin = UV_PWM_GPIO_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(UV_PWM_GPIO_PORT, &GPIO_InitStructure);

    // 配置TIM1时基
    TIM_TimeBaseStructure.TIM_Period = UV_PWM_ARR;              // 自动重装载值99
    TIM_TimeBaseStructure.TIM_Prescaler = UV_PWM_PSC;           // 预分频71
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

    // 配置TIM1_CH3N为PWM模式1,高电平有效
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;  // 使能互补输出
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;        // 主通道高电平有效
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;      // 互补通道高电平有效
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
    TIM_OCInitStructure.TIM_Pulse = 0;                               // 初始占空比0
    TIM_OC3Init(TIM1, &TIM_OCInitStructure);
    TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);

    // 使能TIM1主输出(高级定时器必须)
    TIM_CtrlPWMOutputs(TIM1, ENABLE);

    // 使能TIM1
    TIM_Cmd(TIM1, ENABLE);

    s_uv_current_percent = 0;
}

/**
 * @brief 设置UV灯电流
 * @param percent 电流百分比(0/10/100)
 * @note
 *   0:   关闭 - PA8=高,PB15 PWM关闭
 *   10:  10%电流 - PA8=低,PB15 PWM 10%
 *   100: 100%电流 - PA8=低,PB15 PWM 100%
 */
void app_uv_set_current(uint8_t percent)
{
    switch(percent)
    {
        case UV_CURRENT_OFF:        // 关闭
            // PA8输出高电平(熄灭UV灯)
            GPIO_ResetBits(UV_ENABLE_GPIO_PORT, UV_ENABLE_GPIO_PIN);
            // PB15 PWM关闭
            TIM_SetCompare3(TIM1, 0);
            s_uv_current_percent = 0;
            break;

        case UV_CURRENT_10PCT:      // 10%电流
            // PA8输出低电平(点亮UV灯)
            GPIO_SetBits(UV_ENABLE_GPIO_PORT, UV_ENABLE_GPIO_PIN);
            // PB15 PWM设置10%占空比
            TIM_SetCompare3(TIM1, UV_PWM_CCR_10PCT);
            s_uv_current_percent = 10;
            break;

        case UV_CURRENT_100PCT:     // 100%电流
            // PA8输出低电平(点亮UV灯)
            GPIO_SetBits(UV_ENABLE_GPIO_PORT, UV_ENABLE_GPIO_PIN);
            // PB15 PWM设置100%占空比
            TIM_SetCompare3(TIM1, UV_PWM_CCR_100PCT);
            s_uv_current_percent = 100;
            break;

        default:                    // 非法值,关闭
            GPIO_ResetBits(UV_ENABLE_GPIO_PORT, UV_ENABLE_GPIO_PIN);
            TIM_SetCompare3(TIM1, 0);
            s_uv_current_percent = 0;
            break;
    }
}

/**
 * @brief 停止UV灯工作
 */
void app_uv_stop(void)
{
    // PA8输出高电平(熄灭UV灯)
    GPIO_ResetBits(UV_ENABLE_GPIO_PORT, UV_ENABLE_GPIO_PIN);
    // PB15 PWM关闭
    TIM_SetCompare3(TIM1, 0);
    s_uv_current_percent = 0;
}

/**
 * @brief 获取当前UV灯电流百分比
 */
uint8_t app_uv_get_current(void)
{
    return s_uv_current_percent;
}
